package com.dotools.theme;

import com.dot.zipio.ZipIO;
import com.dotools.utils.FileUtils;
import java.io.File;
import java.io.IOException;

public class ThemeUtils extends ThemeConstance{
    
    
    /**
     * 主题安装以后，启动时，解析资源 并保存至THEME_ZIP_TYPE_EXTREDATA。
     */
    public static void themeExtreData(String sourceFile ,String themePkg,String versionCode){
        String targetFile = THEME_ZIP_PATH + themePkg + THEME_ZIP_TYPE_EXTREDATA;
        ZipIO.instance.doZip(targetFile, sourceFile);
    }
    
    /**
     * 主题中心下载主题apk 完毕以后 copy_white 并 更名到THEME_ZIP_TYPE_APK；
     * @param themePkg
     */
    public static String themeCenterDownLoadCopy(File sourceFile,String themePkg,String version){
        String targetFile = THEME_ZIP_PATH + themePkg + THEME_ZIP_TYPE_APK;
        if(!new File(THEME_ZIP_PATH).exists()){
            new File(THEME_ZIP_PATH).mkdirs();
        }
        try {
            FileUtils.copyFile(sourceFile, new File(targetFile));
            FileUtils.delete(sourceFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return targetFile;
    }

    public static String getThemeZipFileName(String themePkg){
        String themeZipName = THEME_ZIP_PATH + themePkg + THEME_ZIP_TYPE_APK;
        File f = new File(themeZipName);
        if(f.exists() && f.isFile()) {
            return themeZipName;
        }
        themeZipName = THEME_ZIP_PATH + themePkg + THEME_ZIP_TYPE_EXTREDATA;
        f = new File(themeZipName);
        if(f.exists() && f.isFile()) {
            return themeZipName;
        }
        return null;
    }
    public static String getThemeZipType(String themePkg){
        String themeZipName = THEME_ZIP_PATH + themePkg + THEME_ZIP_TYPE_APK;
        File f = new File(themeZipName);
        if(f.exists() && f.isFile()) {
            return THEME_ZIP_TYPE_APK;
        }
        themeZipName = THEME_ZIP_PATH + themePkg + THEME_ZIP_TYPE_EXTREDATA;
        f = new File(themeZipName);
        if(f.exists() && f.isFile()) {
            return THEME_ZIP_TYPE_EXTREDATA;
        }
        return null;
    }
    


    public static String getThemePkgName(String themeZipName) {
        if (themeZipName.endsWith(THEME_ZIP_TYPE_APK)
                || themeZipName.endsWith(THEME_ZIP_TYPE_EXTREDATA)) {
            String str = themeZipName.substring(0,
                    themeZipName.length() - THEME_ZIP_TYPE_APK.length());
            return str;
        }
        return null;
    }
}
